package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.EmployeeQuery;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        //保存的时候进行加密
        String password = employee.getPassword();
        //使用加盐来进行加密
        Md5Hash md5Hash = new Md5Hash(password, employee.getName());
        //把加密过的密码设置进去
        employee.setPassword(md5Hash.toString());
        employeeMapper.insert(employee);
        //再往中间表里插入对应的employee_id和role_id
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                employeeMapper.insertRelation(employee.getId(), id);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //先删除中间表里的这个员工的角色
        employeeMapper.deleteRelation(employee.getId());
        //处理中间表的代码  只有这一个会重复添加
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                employeeMapper.insertRelation(employee.getId(), id);
            }
        }
    }

    @Override
    public Employee login(String username, String password) {
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        if (employee == null) {
            throw new RuntimeException("账号和密码不存在");
        }
        return employee;
    }

    @Override
    public PageInfo list(EmployeeQuery employeeQuery) {
        //使用pageHelper插件
        int currentPage = employeeQuery.getCurrentPage();
        int pageSize = employeeQuery.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        List<Employee> list = employeeMapper.list(employeeQuery);
        return new PageInfo(list);
    }

    @Override
    public void deleteRelation(Long id) {
        employeeMapper.deleteRelation(id);
    }

    @Override
    public Employee selectByName(String name) {
        return employeeMapper.selectByName(name);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        employeeMapper.deleteBatch(ids);
        employeeMapper.batchDeleteRelation(ids);
    }

    @Override
    public Workbook exportXls() {
        HSSFWorkbook web = new HSSFWorkbook();
        HSSFSheet sheet = web.createSheet("员工列表");
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            HSSFRow row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(employee.getName());
            row1.createCell(1).setCellValue(employee.getEmail());
            row1.createCell(2).setCellValue(employee.getAge());
        }
        return web;
    }

    @Override
    public void inportXls(MultipartFile file) throws IOException {
        HSSFWorkbook web = new HSSFWorkbook(file.getInputStream());
        //读取第一页
        HSSFSheet sheet = web.getSheetAt(0);
        //获取最大索引数
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i < lastRowNum; i++) {
            HSSFRow row = sheet.getRow(i + 1);
            Employee employee = new Employee();
            employee.setName(row.getCell(0).getStringCellValue());
            employee.setEmail(row.getCell(1).getStringCellValue());
            //excel是数字类型
            double age = row.getCell(2).getNumericCellValue();
            employee.setAge((int) age);
            //设置默认密码
            employee.setPassword("1");
            this.save(employee, null);
        }
    }
}

